package ${serviceImpl};

import cn.hutool.core.collection.CollectionUtil;
import ${service}.${table.alias}Service;
import ${dto}.${table.alias}DTO;
import ${po}.${table.alias}PO;
import ${manager}.${table.alias}Manager;
import ${param}.${table.alias}Param;
<#if ifNeedStatistics==true>
import ${counts}.CountsDTO;
</#if>
import com.github.pagehelper.PageInfo;
import com.fingard.rh.fgmsp.common.bean.PageView;
import com.fingard.rh.fgmsp.common.bean.ResultContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author ${author}
 * @version 1.0 ${date}
 */
@Slf4j
@Service
public class ${table.alias}ServiceImpl implements ${table.alias}Service {

    @Autowired
    private ${table.alias}Manager ${table.propertyName}Manager;

    @Override
    public PageView<${table.alias}DTO> listByParam(${table.alias}Param ${table.propertyName}Param) {
        PageInfo<${table.alias}DTO> pageInfo = ${table.propertyName}Manager.listByParam(${table.propertyName}Param);
        PageView<${table.alias}DTO> pageView = new PageView<>();
        pageView.setRows(this.translate(pageInfo.getList()));
        pageView.setTotal(pageInfo.getTotal());
        return pageView;
    }

<#if ifNeedStatistics==true>
    @Override
    public ResultContext<CountsDTO> countByParam(${table.alias}Param ${table.propertyName}Param){
        CountsDTO countsDTO = ${table.propertyName}Manager.countByParam(${table.propertyName}Param);
        return new ResultContext<>(countsDTO);
    }
</#if>

    @Override
    public ResultContext<${table.alias}PO> getById(<#if ifSpiltByEnt==true>String entNum,</#if> String id) {
        ${table.alias}PO ${table.propertyName}PO = ${table.propertyName}Manager.queryByPrimaryKey(<#if ifSpiltByEnt==true>entNum,</#if> id);
        return new ResultContext<>(${table.propertyName}PO);
    }

    private <T> List<T> translate(List<T> list) {
        if (CollectionUtil.isNotEmpty(list)) {
            if(list.get(0) instanceof ${table.alias}DTO){
                for (T dto: list) {
                    ${table.alias}DTO ${table.propertyName}DTO = (${table.alias}DTO) dto;
                    this.translate(${table.propertyName}DTO);
                }
            }
        }
        return list;
    }

    private ${table.alias}DTO translate(${table.alias}DTO ${table.propertyName}DTO) {
       <#list table.columnList as map>
           <#if map.property=="CREATOR">
               ${table.propertyName}DTO.setCreatorTranslate(ObjectUtil.defaultIfNull(${table.propertyName}Manager.getById(${table.propertyName}DTO.getCreator()), new UserDTO()).getName());
           <#elseif map.property=="UPDATOR">
               ${table.propertyName}DTO.setUpdatorTranslate(ObjectUtil.defaultIfNull(${table.propertyName}Manager.getById(${table.propertyName}DTO.getUpdator()), new UserDTO()).getName());
           <#else>
           </#if>
       </#list>
       return ${table.propertyName}DTO;
    }
}
